package cz.muni.fi.android.assistivecamera.dialog;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import cz.muni.fi.android.assistivecamera.R;
import cz.muni.fi.android.assistivecamera.texttospeech.SpeechHandler;

public class BlindDialog extends Dialog {

	private String[] arrayOfNames;
	private String title;
	private ListView mListView;
	private ListAdapter mListAdapter;
	private Context ctx;
	private Vibrator vib = null;
	
	private ResponseEventListener responseEventListener;
	
	/**
	* @param context
	*/
	public BlindDialog(Context context, ResponseEventListener responseEventListener, String title, String[] names) {
		super(context);
		
		ctx = context;
		
		vib = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
		
		this.responseEventListener = responseEventListener;
		
		this.title = title;
		arrayOfNames = names;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setTitle(title);
	
		setContentView(R.layout.list);
		
		getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
		mListView = (ListView) findViewById(R.id.list);
		mListAdapter = new ListAdapter(arrayOfNames);
		mListView.setAdapter(mListAdapter);
		
		final MediaPlayer mpButton = MediaPlayer.create(ctx, R.raw.button_click); 
		
		mListView.setOnTouchListener(new OnTouchListener() {
	    	int lastIndex = -2;
	    	
	        @Override
	        public boolean onTouch(View v, MotionEvent event) {
            	final int index = mListView.pointToPosition((int)event.getX(), (int)event.getY());
            	
            	switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                	mpButton.start();
                	if (index != -1 && index == lastIndex) {
                		String text = (String) mListAdapter.getItem(index);
                		responseEventListener.responseEvent(text);
                		text = ctx.getResources().getString(R.string.clicked) + " " + text; 
                		SpeechHandler.ttsQueue(text, 1, 1);
                	}
                    break;
                }

            	if (index != -1 && index != lastIndex) {
            		String text = (String) mListAdapter.getItem(index);
            		SpeechHandler.ttsQueue(text, 0, 1);
            		vib.vibrate(100);
            		lastIndex = index;
            	} else if (index != lastIndex) {
            		lastIndex = index;
            	}
                return true;
	        }
	    });
		
		mListView.setOnKeyListener(new ListView.OnKeyListener() {
			int lastIndex = -2;
			
		    @Override
		    public boolean onKey(View v, int keyCode, KeyEvent keyEvent) {
		    	ListView listView = (ListView) v;
		        if (listView.getSelectedView() != null) {
			        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			        	int index = mListView.getSelectedItemPosition();
			        	if (index != lastIndex) {
			        		String text = (String) mListAdapter.getItem(index);
			        		SpeechHandler.ttsQueue(text, 0, 0);
			        	}
			        	lastIndex = index;
			        }
		        }
		        return false;
		    }
		});
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
	         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	        	 if (position == mListView.getSelectedItemPosition()) {
	        	 	String text = (String) mListAdapter.getItem(position);
	        	 	text = ctx.getResources().getString(R.string.clicked) + " " + text; 
	        		SpeechHandler.ttsQueue(text, 1, 0);
	        		responseEventListener.responseEvent(mListAdapter.getItem(position)); 
	        	 }
	         }
	    });
	}
	
    private class ListAdapter extends BaseAdapter {

        private String[] mData;

        public ListAdapter(String[] data) {
            mData = data;
        }

        @Override
        public int getCount() {
            return mData.length;
        }

        @Override
        public String getItem(int position) {
            return mData[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            TextView result;

            if (convertView == null) {
                result = (TextView) getLayoutInflater().inflate(R.layout.item, parent, false);
            } else {
                result = (TextView) convertView;
            }

            final String command = getItem(position);

            result.setText(command);

            return result;
        }
    }
}
